﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SpectrumAnalyser.Controls.Wpf.Virtualization.Elements
{
    public class SimpleVirtualLine : IVirtualChild
    {
        private readonly Rect _bounds;
        private readonly double _x1;
        private readonly double _x2;
        private readonly double _y1;
        private readonly double _y2;
        private readonly Brush _brush;
        private UIElement _visual;

        public event EventHandler BoundsChanged;

        public SimpleVirtualLine(double x1, double x2, double y1, double y2, Brush brush)
        {
            _x1 = x1;
            _x2 = x2;
            _y1 = y1;
            _y2 = y2;
            _brush = brush;
            _bounds = new Rect(0,0, Math.Abs(x1 - x2),
                               Math.Abs(y1 - y2));
        }

        public Rect Bounds
        {
            get { return _bounds; }
        }

        public UIElement Visual
        {
            get { return _visual; }
        }

        public UIElement CreateVisual(VirtualCanvas parent)
        {
            if (_visual == null)
            {
                _visual = new Line
                    {
                        X1 = _x1,
                        X2 = _x2,
                        Y1 = _y1,
                        Y2 = _y2,
                        Stroke = _brush
                    };
            }
            return _visual;
        }

        public void DisposeVisual()
        {
            _visual = null;
        }
    }
}